ifneq ($(wildcard make.mk),)
include make.mk
endif


all: config test.exe


BUILD_DIR := build
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(CXX_SOURCE:.c=.o)))
vpath %.c $(sort $(dir $(CXX_SOURCE)))


test.exe: $(OBJECTS)
	@echo "LD  $@"
	@$(CC) $^ -o $@


$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
	@echo "CC  $<"
	@$(CC) -c $< -o $@


$(BUILD_DIR):
	mkdir $@



# Pseudo command
.PHONY: config distclean clean menuconfig


# menuconfig command
menuconfig:
	@./lm.exe


# Check if the make.mk file exists
config: make.mk
make.mk:
	@if [ ! -f $@ ]; then \
		echo "Error: Configuration file (make.mk) not found."; \
		$(error Exiting Makefile due to missing make.mk file, please run 'make menuconfig'); \
	fi


# distclean command, delete build directory and make.mk file
distclean:
	rm  -rf $(BUILD_DIR) make.mk


# distclean clean
clean:
	@rm -rf test.exe $(BUILD_DIR)


# xxx_defconfig command, copy configs/xxx_defconfig into current directory
%_defconfig:
	@cp -ra configs/$(MAKECMDGOALS) make.mk
	@./lm.exe -d
	@(exit 0)
